<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>颜色渐变效果</title>
  <style>
    *{
      padding: 0;
      margin: 0;
    }
    body{
      overflow: hidden;
    }

    canvas{
      position: fixed;
      width: 100%;
      height: 100%;
    }
  </style>
</head>
<body>
  <canvas id="canvas">缺点：气泡会越来越多，数组会一直增大，极其影响性能</canvas>
  <script>
    ;(function(doc, win, undefined){
      var canvas = doc.querySelector("#canvas");
      canvas.width = doc.documentElement.clientWidth;
      canvas.height = doc.documentElement.clientHeight;
      var ctx, arcArr = [], flag = 0;
      if(canvas.getContext){
        ctx = canvas.getContext("2d");
        createArc();
        requestAnimationFrame(step);
      }
      
      function step(){
        // 每次画之前清空画布
        ctx.clearRect(0, 0, canvas.width, canvas.height)
        // 每次绘制前都增大圆的半径、逐步减小透明度
        for(var i = 0, length = arcArr.length; i < length; i++){
          arcArr[i].r++;
          arcArr[i].alpha -= 0.01;
        }
        for(var i = 0, length = arcArr.length; i < length; i++){
          drawing(arcArr[i]);
        }
        requestAnimationFrame(step)
      }

      // 创建要绘制的圆的坐标等信息
      function createArc(){
        var timer = setInterval(function(){
          arcArr.push({
            x: Math.random() * canvas.width,
            y: Math.random() * canvas.height,
            r: 20,
            rgb: Math.random()*255+","+Math.random()*255+","+Math.random()*255,
            alpha: 1
          });
        }, 500)
      }
      
      // 画圆
      function drawing(obj){
        ctx.save();
        ctx.fillStyle = "rgba("+obj.rgb+","+obj.alpha+")";
        ctx.beginPath();
        ctx.arc(obj.x, obj.y, obj.r, 0, 2*Math.PI);
        ctx.fill();
        ctx.restore();
      }
      
    }(document, window, undefined));
  </script>
</body>
</html>